package com.mine.distinctionteam.ui.good;
import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import com.bumptech.glide.Glide;
import com.mine.distinctionteam.R;
import com.mine.distinctionteam.bean.goods.GoodsBean;
import com.mine.distinctionteam.bean.goods.GoodsObjBean;
import com.mine.distinctionteam.bean.shopcar.ShopCarBean;
import com.mine.distinctionteam.ui.businessman.BusinessmanActivity;
import com.mine.distinctionteam.ui.good.adapter.GoodsViewPagerAdapter;
import com.mine.distinctionteam.ui.shopnews.ShopNewsActivity;
import com.mine.distinctionteam.widget.CircleTransform;
import com.mine.distinctionteam.widget.MyListView;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import me.relex.circleindicator.CircleIndicator;

public class GoodsActivity extends AppCompatActivity implements GoodsContract.View  {
    @BindView(R.id.viewpager_goods)
    ViewPager viewpagerGoods;
    @BindView(R.id.indicator_goods)
    CircleIndicator indicatorGoods;
    @BindView(R.id.textview_goods_name)
    TextView textviewGoodsName;
    @BindView(R.id.textview_goods_discount)
    TextView textviewGoodsDiscount;
    @BindView(R.id.textview_goods_minus)
    TextView textviewGoodsMinus;
    @BindView(R.id.textview_goods_price)
    TextView textviewGoodsPrice;
    @BindView(R.id.textview_goods_oldprice)
    TextView textviewGoodsOldprice;
    @BindView(R.id.recycler_goods)
    RecyclerView recyclerGoods;
    @BindView(R.id.imageview_goods_head)
    ImageView imageviewGoodsHead;
    @BindView(R.id.textview_goods_nickname)
    TextView textviewGoodsNickname;
    @BindView(R.id.textview_goods_count)
    TextView textviewGoodsCount;
    @BindView(R.id.textview_goods_say)
    TextView textviewGoodsSay;
    @BindView(R.id.textview_goods_summary)
    TextView textviewGoodsSummary;
    @BindView(R.id.listview_goods)
    MyListView listviewGoods;
    @BindView(R.id.imageview_goods_tag)
    ImageView imageviewGoodsTag;
    @BindView(R.id.textview_goods_tag)
    TextView textviewGoodsTag;
    @BindView(R.id.textview_goods_tagsay)
    TextView textviewGoodsTagsay;
    @BindView(R.id.button_goods)
    Button buttonGoods;
    @BindView(R.id.textview_goods_price_botton)
    TextView textviewGoodsPriceBotton;
    @BindView(R.id.textview_goods_oldprice_botton)
    TextView textviewGoodsOldpriceBotton;
    @BindView(R.id.textview_goods_chap_botton)
    TextView textviewGoodsChapBotton;
    @BindView(R.id.button_goods_shop)
    Button buttonGoodsShop;
    @BindView(R.id.layout_goods_head)
    RelativeLayout layoutGoodsHead;
    private Context context = this;
    private GoodsPresenter presenter;
    private String id;
    private GoodsObjBean objBean;
    private boolean isSave = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goods);
        ButterKnife.bind(this);
        final Intent intent = getIntent();
        presenter = new GoodsPresenter(this);
        id = intent.getStringExtra("id");
        presenter.getGoods(id);
        layoutGoodsHead.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent1=new Intent(context, BusinessmanActivity.class);
                intent1.putExtra("userid",objBean.getFromUser().getId());
                startActivity(intent1);
            }
        });
        buttonGoods.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent shop = new Intent(context, ShopNewsActivity.class);
                String link = objBean.getRemind().getLink();
                Bundle bundle = new Bundle();
                bundle.putString("link", link);
                shop.putExtras(bundle);
                startActivity(shop);
            }
        });
//
//        buttonGoodsShop.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                if (objBean != null&&objBean.isIsOnsale()) {
//                    ShopCarBean shopCarBean = new ShopCarBean(objBean.getBrandName(), objBean.getGoodsPics().get(0), objBean.getId(), objBean.getGoodsName(), objBean.getSellPrice(), objBean.getOriginalPrice(), false);
//                    List<ShopCarBean> shopList = ShopCarDao.quaryAll(context);
//                    if (shopList != null && shopList.size() > 0) {
//                        //判断是否收藏过
//                        for (int i = 0; i < shopList.size(); i++) {
//                            if (shopList.get(i).getShopid() == shopCarBean.getShopid()) {
//                                isSave = true;
//                            }
//                        }
//                        //通过判断进行收藏
//                        if (!isSave) {
//                            ShopCarDao.add(shopCarBean, context);
//                            Toast.makeText(context, "添加成功", Toast.LENGTH_SHORT).show();
//                        } else {
//                            Toast.makeText(context, "已经添加过了", Toast.LENGTH_SHORT).show();
//                        }
//                    } else if (shopList == null || shopList.size() == 0) {
//                        //如果没有数据直接收藏
//                        ShopCarDao.add(shopCarBean, context);
//                        Toast.makeText(context, "添加成功", Toast.LENGTH_SHORT).show();
//                    }
//                }
//            }
//        });
    }
    @Override
    public void getGoodsSucess(GoodsBean bean) {
        objBean = bean.getDetail().getGoodsObj();
        GoodsViewPagerAdapter adapter = new GoodsViewPagerAdapter(context, objBean.getGoodsPics());
        viewpagerGoods.setAdapter(adapter);
        indicatorGoods.setViewPager(viewpagerGoods);

        String name;
        if (!objBean.getConditionDis().isEmpty()) {
            name = "[" + objBean.getConditionDis() + "]" + objBean.getGoodsName();
        } else {
            name = objBean.getGoodsName();
        }
        textviewGoodsName.setText(name);

        double price = Double.valueOf(objBean.getSellPrice());
        double oldprice = Double.valueOf(objBean.getOriginalPrice());
        double discount = oldprice - price;
        double minus = price / oldprice;
        DecimalFormat df = new DecimalFormat("#.00");
        String format = df.format(minus * 10);
        textviewGoodsMinus.setText("省￥" + discount);
        textviewGoodsDiscount.setText(format + "折");
        textviewGoodsPrice.setText("￥" + price);
        textviewGoodsPriceBotton.setText("￥" + price);
        textviewGoodsOldprice.setText("￥" + oldprice);
        DecimalFormat df2 = new DecimalFormat("#");
        double minus1=(100 - (minus * 100));
        textviewGoodsChapBotton.setText("节省" + df2.format(minus1) + "%");
        textviewGoodsOldpriceBotton.setText("￥" + oldprice);
        textviewGoodsOldprice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);

        Glide.with(context).load(objBean.getFromUser().getHeadimg()).transform(new CircleTransform(context)).into(imageviewGoodsHead);
        textviewGoodsNickname.setText(objBean.getFromUser().getNickname());
        textviewGoodsCount.setText("共发布了" + objBean.getFromUser().getPublishCount() + "商品，售出" + objBean.getFromUser().getSoldCount() + "件");
        textviewGoodsSay.setText(objBean.getPublisherDesc());
        textviewGoodsSummary.setText(objBean.getPurityDesc());

        List<Map<String, String>> goodsList = new ArrayList<>();
        for (int i = 0; i < objBean.getDetailParams().size(); i++) {
            Map<String, String> map = new HashMap<>();
            map.put("key", objBean.getDetailParams().get(i).getKey());
            map.put("value", objBean.getDetailParams().get(i).getValue());
            goodsList.add(map);
        }
        SimpleAdapter simpleAdapter = new SimpleAdapter(context, goodsList, R.layout.item_goods_listview, new String[]{"key", "value"}, new int[]{R.id.textview_goods_listview_key, R.id.textview_goods_listview_value});
        listviewGoods.setAdapter(simpleAdapter);

        Glide.with(context).load(objBean.getBrandObj().getLogoUrl()).into(imageviewGoodsTag);
        textviewGoodsTag.setText(objBean.getBrandObj().getName());
        textviewGoodsTagsay.setText(objBean.getBrandObj().getDescription());
        if (!objBean.isIsOnsale()){
            buttonGoodsShop.setText("已售出");
            buttonGoodsShop.setBackgroundColor(getResources().getColor(R.color.darkgray));
        }
    }

    @Override
    public void getGoodsFail(String erro) {
        Toast.makeText(context, erro, Toast.LENGTH_SHORT).show();
    }
}
